package com.six.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.six.dao.ImgsDaoImpI;
import com.six.dao.ProductDaoImpl;
import com.six.dao.ProducttypeDaoImpl;

import com.six.entity.Imgs;
import com.six.entity.Product;
import com.six.entity.producttype;

/**
 * Servlet implementation class ProducttypeServlet
 */
@WebServlet("/ProducttypeServlet")
public class ProducttypeServlet extends BaseServlet {

	// 产品类型
	public void proType(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String protype = req.getParameter("protype");
		int typeIdInt = 0;
		if (protype != null && !protype.equals("")) {
			typeIdInt = Integer.valueOf(protype);
		}
		ProducttypeDaoImpl dao = new ProducttypeDaoImpl();
		ArrayList<producttype> typee = dao.getAllProType();

		ProductDaoImpl prodao = new ProductDaoImpl();
		ArrayList<Product> proList = prodao.getListByTypeId(typeIdInt);

		req.setAttribute("prolist", proList);
		req.setAttribute("protype", typee);
		System.out.println(typee);

		req.getRequestDispatcher("productlist.jsp").forward(req, resp);

	}
	/**
	 * 产品详情
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void doProductDetails(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		// 通过name获取值
		String id = req.getParameter("id");

		// 产品类型列表
		ProducttypeDaoImpl Protype = new ProducttypeDaoImpl();
		ArrayList<producttype> allProtype = Protype.getAllProType();

		int ids = 0;
		if (id != null && !id.equals("")) {
			ids = Integer.valueOf(id);
		}
		// 图片列表
		ImgsDaoImpI imgsDao = new ImgsDaoImpI();
		ArrayList<Imgs> imgslist = imgsDao.getImagesList(ids);

		// 产品列表
		ProductDaoImpl proDao = new ProductDaoImpl();
		ArrayList<Product> pro = proDao.getAllList();

		req.setAttribute("allProtype", allProtype);
		req.setAttribute("pro", pro);
		req.setAttribute("imglist", imgslist);

		// 跳转页面
		req.getRequestDispatcher("productdetails.jsp").forward(req, resp);
	}
}
